package cn.com.generaldata.os.service.common;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.Role;

@Component
public class RoleService {

	private static Logger log = LoggerFactory.getLogger(RoleService.class);
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Role> roleDAO;


	public String createRole(Role role) {
		if (role != null) {
			if (StringUtils.isNotBlank(role.getRoleName())) {
				Role oldRole = checkRoleName(role.getRoleName());
				if (oldRole != null) {
					// throw new CommonException(
					// ExceptionConstant.CODE_DUPLICATE_ROLE_NAME,
					// ExceptionConstant.CAUSE_CREATE_ROLE);
					log.error("oldRole为空");
					return null;
				}
			}
			if (role.getId() == null || "".equals(role.getId()))
				role.setId(UUID.randomUUID().toString().replaceAll("-", ""));

			roleDAO.save(role);
			return role.getId();
		}
		return null;
	}


	public Role modifyRole(Role role) {
		if (role != null) {
			roleDAO.update(role);
			return role;
		}
		return null;
	}


	public Role queryRoleById(String id) {
		if (id != null) {
			return roleDAO.get(Role.class, id);
		}
		return null;
	}


	public Result queryRoles(Filter filter, Page page) {
		List<Role> roles = roleDAO.find(Role.class, filter, page);
		int count = roleDAO.count(Role.class, filter);

		if (page == null) {
			return new Result(1, 1, count, roles, new ArrayList<Event>());
		}
		return new Result(page.getPageNumber(), page.getPageSize(), count, roles, new ArrayList<Event>());
	}
	

	public void removeRoles(String[] ids) {
		if (ids != null && ids.length > 0) {

			Filter filter = new Filter();
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", ids));

			List<Role> roles = roleDAO.find(Role.class, filter, null);
			roleDAO.delete(roles);
		}
	}
	
	/**
	 * 取得所有的角色.
	 * @return
	 */
	public List<Role> queryAllRoles() {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("roleName");

		filter.addRule(new FilterRule(FilterConstant.NOT_EQUAL, "roleDesc", "Root"));
		
		return roleDAO.find(Role.class, filter,null);
	}

	private Role checkRoleName(String name) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "roleName", name));

		List<Role> roles = roleDAO.find(Role.class, filter, null);
		if (roles.size() > 0) {
			return roles.get(0);
		}
		return null;
	}

}
